package com.hy.study.utils;

/**
 * 使用枚举优化多分支语句
 *
 * @author huyong
 * @date 2024 年 01 月 12 日 10:14: 18
 */

public enum SaveStrategyEnum {
    DB("db") {
        @Override
        public void save(Object data) {
            System.out.println("保存数据库");
        }
    },
    DISK("disk") {
        @Override
        public void save(Object data) {
            System.out.println("保存到磁盘");
        }
    },
    OSS("oss") {
        @Override
        public void save(Object data) {
            System.out.println("保存到OSS");
        }
    };
    private final String type;

    SaveStrategyEnum(String type) {
        this.type = type;
    }

    public abstract void save(Object data);

    public static SaveStrategyEnum typeOf(String type) {
        for (SaveStrategyEnum strategyEnum : values()) {
            if (strategyEnum.type.equalsIgnoreCase(type)) {
                return strategyEnum;
            }
        }
        return null;
    }

    private void save(String type, Object data) {
        SaveStrategyEnum strategyEnum = typeOf(type);
        if (strategyEnum != null) {
            strategyEnum.save(data);
        }
    }


}
